package com.doaio.service.link.impl;

import com.doaio.constant.ErrorConstant;
import com.doaio.dao.LinkDao;
import com.doaio.dto.LinkDto;
import com.doaio.exception.BusinessException;
import com.doaio.model.LinkDomain;
import com.doaio.service.link.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class LinkServiceImpl implements LinkService {

    @Autowired
    private LinkDao linkDao;

    @Override
    @Transactional
    @CacheEvict(value={"linkCaches","linkCache"},allEntries=true,beforeInvocation=true)
    public void addLink(LinkDomain link) {
        if(null == link)
            throw BusinessException.withErrorCode(ErrorConstant.Common.PARAM_IS_EMPTY);
        linkDao.addLink(link);
    }

    @Override
    public void saveLink(String type, String name, Integer mid) {

    }

    @Override
    public void addLinks(Integer cid, String names, String type) {

    }

    @Override
    public void saveOrUpdate(Integer cid, String name, String type) {

    }

    @Override
    @CacheEvict(value={"linkCaches","linkCache"},allEntries=true,beforeInvocation=true)
    public void deleteLinkById(Integer lid) {
        linkDao.deleteLinkById(lid);
    }

    @Override
    @CacheEvict(value={"linkCaches","linkCache"},allEntries=true,beforeInvocation=true)
    public void updateLink(LinkDomain link) {
        if (null == link || null == link.getLid())
            throw BusinessException.withErrorCode(ErrorConstant.Common.PARAM_IS_EMPTY);
        linkDao.updateLink(link);
    }

    @Override
    public LinkDomain getLinkById(Integer mid) {
        return null;
    }

    @Override
    @Cacheable(value = "linkCaches")
    public List<LinkDomain> getLinks() {
        return linkDao.getLinks();
    }

    @Override
    public List<LinkDto> getLinkList(String type, String orderBy, int limit) {
        return null;
    }
}
